package com.imti.mall.order.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.order.dto.ReturnApplyParam;
import com.imti.mall.model.order.pojo.OmsApplyAudit;
import com.imti.mall.model.order.pojo.OmsCompanyAddress;
import com.imti.mall.model.order.pojo.OmsOrderReturnApply;
import com.imti.mall.model.order.query.ReturnApplyQuery;
import com.imti.mall.order.service.OmsCompanyAddressService;
import com.imti.mall.order.service.OmsOrderReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "退货管理")
@RequestMapping("/api/v1/orders/returnApply")
public class ReturnApplyController {

  @Autowired
  private OmsOrderReturnApplyService omsOrderReturnApplyService;
  @Autowired
  private OmsCompanyAddressService omsCompanyAddressService;

  @ApiOperation(value = "查询退货列表")
  @GetMapping
  public CommonResult list(ReturnApplyQuery returnApplyQuery,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

    List<OmsOrderReturnApply> omsOrderReturnApplyList = omsOrderReturnApplyService.list(returnApplyQuery, pageSize, pageNum);
      return CommonResult.success(CommonPage.restPage(omsOrderReturnApplyList));

  }

  @ApiOperation("根据ID获取退货详情")
  @GetMapping(value = "/{id}")
  public CommonResult<OmsOrderReturnApply> detailApply(@PathVariable Long id) {
    OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyService.getById(id);
    return CommonResult.success(omsOrderReturnApply);
  }

  @ApiOperation("获取前端页面传递的退货申请并存入数据库相关表")
  @PostMapping("/addOmsOrderReturnApply")
  public CommonResult addOmsOrderReturnApply(@RequestBody ReturnApplyParam ReturnApplyParam){
    int result = omsOrderReturnApplyService.addOmsOrderReturnApply(ReturnApplyParam);
    if(result>0){
      return CommonResult.success(1,"添加成功");
    }else{
      return CommonResult.failed("系统繁忙，请稍后再试");
    }
  }

  @ApiOperation("取消退货申请")
  @PostMapping("/delete")
  public CommonResult deleteApply(@RequestParam("ids") List<Long> idList){
    boolean result = omsOrderReturnApplyService.removeByIds(idList);
    if(result){
      return CommonResult.success(1);
    }else{
      return CommonResult.failed("删除失败");
    }
  }

  @ApiOperation("退货流程修改退货状态")
  @PostMapping("/update/status/{id}")
  public CommonResult updateApplyStatus(@PathVariable Long id,@RequestBody OmsApplyAudit omsApplyAudit){
    int result = omsOrderReturnApplyService.updateStatusById(id,omsApplyAudit);
    if(omsApplyAudit.getStatus()==2){
      //退货已完成，提醒用户上传退货信息
    }
    if(result>0){
      return CommonResult.success(1);
    }else{
      return CommonResult.success(0);
    }
  }

  @ApiOperation("添加退货地址信息")
  @GetMapping("/addReseieveMessage")
  public CommonResult addReseieveMessage(@RequestBody OmsCompanyAddress omsCompanyAddress){
    boolean result = omsCompanyAddressService.save(omsCompanyAddress);
    if(result){
      return CommonResult.success(1,"操作成功");
    }else{
      return CommonResult.failed("操作失败");
    }
  }
}
